﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace ShoppingList
{
    public class Common
    {
        public static int Show(string messageBoxText, string caption, string button1, string button2)
        {
            int? returned = null;
            using (var mre = new System.Threading.ManualResetEvent(false))
            {
                string[] buttons;
                if (button2 == null)
                    buttons = new string[] { button1 };
                else
                    buttons = new string[] { button1, button2 };

                Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
                    caption,
                    messageBoxText,
                    buttons,
                    0, // can choose which button has the focus
                    Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.Warning, // can play sounds
                    result =>
                    {
                        returned = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
                        mre.Set(); // could have done it all without blocking
                    }, null);

                mre.WaitOne();
            }

            if (!returned.HasValue)
                return 0;
            else if (returned == 0)
                return 1;
            else if (returned == 1)
                return 2;
            else
                return 0;
        }

        public static bool ConfirmDialog(string title, string question)
        {
            int? returned = null;
            using (var mre = new System.Threading.ManualResetEvent(false))
            {
                string[] buttons = { Resources.ButtonConfirm, Resources.ButtonDoNotConfirm };

                Microsoft.Xna.Framework.GamerServices.Guide.BeginShowMessageBox(
                    title,
                    question,
                    buttons,
                    0, // can choose which button has the focus
                    Microsoft.Xna.Framework.GamerServices.MessageBoxIcon.Alert, // can play sounds
                    result =>
                    {
                        returned = Microsoft.Xna.Framework.GamerServices.Guide.EndShowMessageBox(result);
                        mre.Set(); // could have done it all without blocking
                    }, null);

                mre.WaitOne();
            }

            if (!returned.HasValue)
                return false;
            else if (returned == 0)
                return true;
            else if (returned == 1)
                return false;
            else
                return false;
        }
    }
}